package lv.accenture.ex06;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ProcessingMain {
	public static void writeJSON(String fileName, List<Bookmark> bmrks) throws IOException
	{
		  FileOutputStream fis = new FileOutputStream(fileName);
		  OutputStreamWriter osw = new OutputStreamWriter(fis, "UTF-8");
		  BufferedWriter bw = new BufferedWriter(osw);
			    
		  bw.write("{\"items\" : [\n"); // write preamble of JSON file
		  for (Bookmark b: bmrks) {
		    bw.write(b.toString()); // output each bookmark
		  }
		  bw.write("]}"); // write closing part of JSON
		  bw.close();
		  osw.close();
		  fis.close();
	}
	
	
	
	public static void main(String[] args) throws IOException {
		IResourceList rList = new FSResourceList("src/main/resources/retiman");
		String[] resources = rList.getAllResources();
		System.out.println("total # of tags is " + resources.length);
		Bookmark bm = new Bookmark();
		RegexExtractor extractor = new RegexExtractor();
		List<Bookmark> bms = new ArrayList<Bookmark>();

		for (int i = 0; i < resources.length; i++) {
			
			List<Bookmark> bmRes = extractor.extractBookmarks(resources[i]);
			for (Bookmark b: bmRes)
			{
				bms.add(b);
			}
			
			
			

				}
		writeJSON("bookmarks.js",bms);



	}
}
